<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>计算 1 到输入数的和</title>
</head>

<body>
    <input type="text" id="inputNum" placeholder="请输入一个数">
    <button onclick="sumUp()">计算和</button>
    <div id="sumResult"></div>
    <script>
        function sumUp() {
            var input = document.getElementById('inputNum').value;
            var resultDiv = document.getElementById('sumResult');
            if (!isNaN(parseFloat(input)) && isFinite(input)) {
                var num = parseInt(input);
                var sum = 0;
                for (var i = 1; i <= num; i++) {
                    sum += i;
                }
                resultDiv.innerHTML = "从 1 到 " + num + " 的和是: " + sum;
            } else {
                resultDiv.innerHTML = "输入不是有效的数字";
            }
        }
    </script>
</body>

</html>